import Modal from './modal'
import wepy from 'wepy'

export default class Navigation {
  static tabUrls = ['/pages/index', '/pages/goods/index', '/pages/cart/index', '/pages/user/index'];
  static isTab (url) {
    return this.tabUrls.some(path => path === url)
  }
  /**
   * 如果能够后退（多层），则navigateBack，否则调用redirectTo
   */
  static backOrRedirect(url) {
    if (this.isTab(url)) {
      wx.switchTab({
        url: url
      })
    } else {
      const pages = getCurrentPages()
      // route在低版本不兼容
      const index = pages.findIndex(item => ('/' + item.__route__) === url)
      if (pages.length < 2 || index < 0) {
        wx.redirectTo({
          url: url
        })
      } else {
        const delta = pages.length - 1 - index
        wx.navigateBack({
          delta: delta
        })
      }
    }
  }
  /**
   * 如果能够后退（多层），则navigaetBack，否则调用navigateTo
   */
  static backOrNavigate(url) {
    if (this.isTab(url)) {
      wx.switchTab({
        url: url
      })
    } else {
      const pages = getCurrentPages()
      // route在低版本不兼容
      const index = pages.findIndex(item => ('/' + item.__route__) === url)
      if (pages.length < 2 || index < 0) {
        wx.navigateTo({
          url: url
        })
      } else {
        const delta = pages.length - 1 - index
        wx.navigateBack({
          delta: delta
        })
      }
    }
  }

}
